﻿
@{
    ViewBag.Title = "Show";
}

<style type="text/css">
    .modal.fade.in {
        top: 25%;
    }
</style>

<div class="panel-body" style="padding-bottom:0px;">
    <div class="panel panel-default">
        <div class="panel-heading">查询条件</div>
        <div class="panel-body">
            <form id="formSearch" class="form-horizontal">
                <div class="form-group" style="margin-top:15px">
                    <label class="control-label col-sm-1" for="txt_search_departmentname">部门名称</label>
                    <div class="col-sm-3">
                        <input type="text" class="form-control" id="Name">
                    </div>
                    <label class="control-label col-sm-1" for="txt_search_statu">部门代码</label>
                    <div class="col-sm-3">
                        <input type="text" class="form-control" id="Code">
                    </div>
                    <div class="col-sm-4" style="text-align:left;">
                        <button type="button" style="margin-left:50px" onclick="Query()" id="btn_select" class="btn btn-primary">查询</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div id="toolbar" class="btn-group">
        <input type="hidden" id="index" value="" />
        <button id="btn_add" type="button" class="btn btn-default" data-target="#myModal" data-toggle="modal">
            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
        </button>
        <button id="btn_edit" type="button" class="btn btn-default" onclick="up()">
            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
        </button>
        <button id="btn_delete" type="button" class="btn btn-default" onclick="del()">
            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
        </button>
        <button id="btn_delete" type="button" class="btn btn-default" onclick="Excel()">
            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>导出
        </button>
    </div>
    <table id="tb_departments"></table>
</div>

@*模态框*@
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel"></h4>
            </div>
            <div class="modal-body">

                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">职位名称:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" id="name" placeholder="请输入">
                        </div>
                        <label for="lastname" class="col-sm-2 control-label">职位代码:</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" id="code" placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group">

                    </div>
                    <div class="form-group">
                        @*<div class="col-sm-offset-2 col-sm-10">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox">请记住我
                                    </label>
                                </div>
                            </div>*@
                    </div>
                </form>

            </div>
            <div class="modal-footer" style="text-align:center">
                <button type="button" class="btn btn-default" id="close" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="Add()">提交更改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

@section Scripts{
    <script src="~/Content/bootstrap/dist/bootstrap-table.js"></script>
    <script src="~/Content/bootstrap/dist/locale/bootstrap-table-zh-CN.js"></script>
    <link href="~/Content/bootstrap/dist/bootstrap-table.css" rel="stylesheet" />
    <script src="~/Content/layer/layer.js"></script>
    <link href="~/Content/layer/mobile/need/layer.css" rel="stylesheet" />
    <script>
        $(function () {
            table();
        })

        $("#btn_add").click(function () {
            $("#index").val("");
            $("#name").val("");
            $("#code").val("");
        })
        //添加
        function Add() {
            var str = "";
            if ($("#name").val() == "") {
                str += "职位名称不能为空\n";
            }
            if ($("#code").val() == "") {
                str += "职位代码不能为空\n";
            }
            if (str != "") {
                layer.msg(str);
                return false;
            }

            $.ajax({
                url: "/Home/Add",
                data: {
                    name: $("#name").val(),
                    code: $("#code").val(),
                    index: $("#index").val()
                },
                success: function (data) {
                    if (data) {
                        layer.msg("成功");
                        $("#close").click();//关闭弹窗
                        Query();//刷新
                    }
                    else {
                        layer.msg("失败");
                    }

                }
            })

        }

        function table() {
            $('#tb_departments').bootstrapTable({
                //url: '/Home/GetDepartment',         //请求后台的URL（*）
                url: '/Home/ShowList',         //请求后台的URL（*）
                method: 'get',                      //请求方式（*）
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: true,                   //是否显示分页（*）
                sortable: true,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                queryParams: function (params) {
                    params.Name = $("#Name").val();
                    params.Code = $("#Code").val();
                    return params;
                },//传递参数（*）
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 2,                       //每页的记录行数（*）
                pageList: [2, 25, 50, 100],        //可供选择的每页的行数（*）
                search: true,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                strictSearch: true,
                showColumns: true,                  //是否显示所有的列
                showRefresh: false,                  //是否显示刷新按钮
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "ID",                     //每一行的唯一标识，一般为主键列
                showToggle: true,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: false,                   //是否显示父子表
                columns: [{
                    checkbox: true
                }, {
                    field: 'Id',
                    title: '编号',
                    sortable: true
                }, {
                    field: 'Name',
                    title: '部门名称',
                    sortable: true
                }, {
                    field: 'Code',
                    title: '部门代码',
                    sortable: true
                }],
            });
        }
        //查询
        function Query() {
            $("#tb_departments").bootstrapTable('refresh')
        }
        //删除
        function del() {
            var data = $("#tb_departments").bootstrapTable('getSelections', function (row) {
                return row;
            })
            if (data == "") {
                layer.msg("请选择");
                return false;
            }
            var str = [];
            for (var i = 0; i < data.length; i++) {
                str.push(data[i].Id);
            }
            layer.msg('是否删除？', {
                time: 0 //不自动关闭
                , btn: ['确定', '取消']
                , yes: function (index) {
                    layer.close(index);

                    $.ajax({
                        url: "/Home/Del"
                        , data: {
                            id: str.toString()
                        }
                        , success: function (res) {
                            if (res) {
                                layer.msg("成功");
                                //刷新页面
                                Query();
                            }
                            else {
                                layer.msg("失败");
                            }
                        }
                    })
                }
            });
        }

        function Excel() {
            window.location.href = '/Home/Excel';

        }

        function up() {
            var data = $("#tb_departments").bootstrapTable('getSelections', function (row) {
                return row;
            })

            if (data == "") {
                layer.msg("请选择");
                return false;
            } else if (data.length != 1) {
                layer.msg("只能选择一个");
                return false;
            }


            $("#btn_add").click();
            $("#index").val(data[0].Id);
            if ($("#index").val() != "") {
                $("#name").val(data[0].Name);
                $("#code").val(data[0].Code);
            }


        }
    </script>
}
